#include "mvc/view/configuration_dialog.h"
#include "ui_configuration_dialog.h"

#include <iostream>

#include <QRegExpValidator>

Configuration_dialog::Configuration_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Configuration_dialog)
{
    ui->setupUi(this);
    default_values();
    closed = false;

    set_regular_expresions();

    connect(ui->simulation_button, SIGNAL(clicked()), this, SLOT(get_configurations_parameters()));
    connect(ui->default_button, SIGNAL(clicked()), this, SLOT(default_values()));
    connect(ui->cancel_button, SIGNAL(clicked()), this, SLOT(close()));
}

Configuration_dialog::~Configuration_dialog()
{
    delete ui;
}


/**
  * Set text in the widget.
  */
void Configuration_dialog::retranslateUi(){
    ui->retranslateUi(this);
}

/**
 * @brief Configuration_dialog::getClosed
 * @return the value of the atribute 'closed'
 */
bool Configuration_dialog::getClosed(){
    return closed;
}
//
// PRIVATE SLOTS
//
/*
 * Set a regular expresion for some field
 */
void Configuration_dialog::set_regular_expresions(){
    QRegExp float_field("(0|[1-9][0-9]*)\\.[0-9]+");

    ui->vector_direccion_x->setValidator(new QRegExpValidator(float_field,this));
    ui->vector_direccion_y->setValidator(new QRegExpValidator(float_field,this));
    ui->vector_direccion_z->setValidator(new QRegExpValidator(float_field,this));
    ui->velocidad_caracteristica->setValidator(new QRegExpValidator(float_field,this));
    ui->longitud_caracteristica->setValidator(new QRegExpValidator(float_field,this));

}
/*
 * Reset all fields to default values
 */
void Configuration_dialog::default_values()
{
    ui->offset_box->setValue(2);
    ui->execution_mode_b->setChecked(true);
    ui->yes_matriz_button->setChecked(true);
    ui->no_normal_button->setChecked(true);
    ui->no_velocity_button->setChecked(true);
    ui->sim_all_configs->setChecked(false);
    ui->view_angle_box->setValue(5);
    ui->vector_direccion_x->setText("-1");
    ui->vector_direccion_y->setText("0");
    ui->vector_direccion_z->setText("0");
    ui->velocidad_caracteristica->setText("204.18");
    ui->longitud_caracteristica->setText("0.5");
    ui->t_box->setValue(10);
    ui->nmax_box->setValue(100);
    ui->chord_box->setValue(2.5);
    ui->span_box->setValue(64.5);
    ui->position_box->setValue(0.8);
    ui->nthreads_box->setValue(10);
    ui->process_name->setText("uav");

}

/*
 * Read de values of the widget
 */
void Configuration_dialog::get_configurations_parameters()
{
    configurations_parameters_uavs *cpu = new configurations_parameters_uavs;
    cpu->offset = ui->offset_box->value();
    if(ui->execution_mode_b->isChecked())
        cpu->mode = true;
    else
        cpu->mode = false;
    if(ui->yes_matriz_button->isChecked())
        cpu->matriz = true;
    else
        cpu->matriz = false;
    if(ui->yes_normal_button->isChecked())
        cpu->normal = true;
    else
        cpu->normal = false;
    if(ui->yes_velocity_button->isChecked())
        cpu->velocity = true;
    else
        cpu->velocity = false;
    cpu->sim_all_configs = ui->sim_all_configs->isChecked();
    cpu->view_angle   = ui->view_angle_box->value();
    cpu->vector_direccion_x = ui->vector_direccion_x->text().toFloat();
    cpu->vector_direccion_y = ui->vector_direccion_y->text().toFloat();
    cpu->vector_direccion_z = ui->vector_direccion_z->text().toFloat();
    cpu->velocidad_caracteristica = ui->velocidad_caracteristica->text().toFloat();
    cpu->longitud_caracteristica = ui->longitud_caracteristica->text().toFloat();
    cpu->t_box        = ui->t_box->value();
    cpu->nmax_box     = ui->nmax_box->value();
    cpu->chord_box    = ui->chord_box->value();
    cpu->span_box     = ui->span_box->value();
    cpu->position_box = ui->position_box->value();
    cpu->nthreads_box = ui->nthreads_box->value();
    cpu->process_name = ui->process_name->text();

    closed = true;
    emit s_load_configurations_parameters(cpu);
}

/*
/ Set the configuration parameter values on the Dialog
*/
void Configuration_dialog::set_values(configurations_parameters_uavs *cpu){
    ui->offset_box->setValue(cpu->offset);

    ui->execution_mode_b->setChecked(cpu->mode);
    ui->yes_matriz_button->setChecked(cpu->matriz);
    ui->yes_normal_button->setChecked(cpu->normal);
    ui->yes_velocity_button->setChecked(cpu->velocity);
    ui->sim_all_configs->setChecked(false);

    ui->view_angle_box->setValue(cpu->view_angle);
    ui->vector_direccion_x->setText(QString::number(cpu->vector_direccion_x));
    ui->vector_direccion_y->setText(QString::number(cpu->vector_direccion_y));
    ui->vector_direccion_z->setText(QString::number(cpu->vector_direccion_z));
    ui->velocidad_caracteristica->setText(QString::number(cpu->velocidad_caracteristica));
    ui->longitud_caracteristica->setText(QString::number(cpu->longitud_caracteristica));
    ui->t_box->setValue(cpu->t_box);
    ui->nmax_box->setValue(cpu->nmax_box);
    ui->chord_box->setValue(cpu->chord_box);
    ui->span_box->setValue(cpu->span_box);
    ui->position_box->setValue(cpu->position_box);
    ui->nthreads_box->setValue(cpu->nthreads_box);
    ui->process_name->setText("uav");
}
